home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / content / geshi.php < prev    next >
PHP Script  |  2008-07-06  |  3KB  |  88 lines

  1. <?php
  2. /**
  3. * @version        $Id: geshi.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package        Joomla
  5. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6. * @license        GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13.  
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16.  
  17. $mainframe->registerEvent( 'onPrepareContent', 'plgContentGeshi' );
  18.  
  19. /**
  20. * Code Highlighting Plugin
  21. *
  22. * Replaces <pre>...</pre> tags with highlighted text
  23. */
  24. function plgContentGeshi( &$row, &$params, $page=0 )
  25. {
  26.     // simple performance check to determine whether bot should process further
  27.     if ( JString::strpos( $row->text, 'pre>' ) === false ) {
  28.         return true;
  29.     }
  30.  
  31.     // Get Plugin info
  32.      $plugin =& JPluginHelper::getPlugin('content', 'geshi');
  33.  
  34.     // define the regular expression for the bot
  35.     $regex = "#<pre xml:\s*(.*?)>(.*?)</pre>#s";
  36.  
  37.     $GLOBALS['_MAMBOT_GESHI_PARAMS'] =& $params;
  38.  
  39.     // perform the replacement
  40.     $row->text = preg_replace_callback( $regex, 'plgContentGeshi_replacer', $row->text );
  41.  
  42.     return true;
  43. }
  44. /**
  45. * Replaces the matched tags an image
  46. * @param array An array of matches (see preg_match_all)
  47. * @return string
  48. */
  49. function plgContentGeshi_replacer( &$matches )
  50. {
  51.     $params =& $GLOBALS['_MAMBOT_GESHI_PARAMS'];
  52.  
  53.     jimport('geshi.geshi');
  54.     jimport('domit.xml_saxy_shared');
  55.  
  56.     $args = SAXY_Parser_Base::parseAttributes( $matches[1] );
  57.     $text = $matches[2];
  58.  
  59.     $lang    = JArrayHelper::getValue( $args, 'lang', 'php' );
  60.     $lines    = JArrayHelper::getValue( $args, 'lines', 'false' );
  61.  
  62.  
  63.     $html_entities_match = array( "|\<br \/\>|", "#<#", "#>#", "|'|", '#"#', '# #' );
  64.     $html_entities_replace = array( "\n", '<', '>', "'", '"', ' ' );
  65.  
  66.     $text = preg_replace( $html_entities_match, $html_entities_replace, $text );
  67.  
  68.     $text = str_replace('<', '<', $text);
  69.     $text = str_replace('>', '>', $text);
  70.  
  71. /*
  72.     // Replace 2 spaces with "  " so non-tabbed code indents without making huge long lines.
  73.     $text = str_replace("  ", "  ", $text);
  74.     // now Replace 2 spaces with "  " to catch odd #s of spaces.
  75.     $text = str_replace("  ", "  ", $text);
  76. */
  77.     // Replace tabs with "   " so tabbed code indents sorta right without making huge long lines.
  78.     //$text = str_replace("\t", "   ", $text);
  79.     $text = str_replace( "\t", '  ', $text );
  80.  
  81.     $geshi = new GeSHi( $text, $lang );
  82.     if ($lines == 'true') {
  83.         $geshi->enable_line_numbers( GESHI_NORMAL_LINE_NUMBERS );
  84.     }
  85.     $text = $geshi->parse_code();
  86.  
  87.     return $text;
  88. }